/**
 * 
 */
package com.jyothsna.project1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author Jyothsna
 * COSC 603 Project 1
 */
public class Stutter {

	/**
	 * @param args
	 * @throws IOException
	 * This program looks for repeated words line by line in the file and prints the repeated words in the console
	 */
	public static void main(String[] args) throws IOException {

		try { // surrounded by the try and catch block
			/**
			 *  Opens the file from the location specified			 
			 **/
			FileInputStream fstream = new FileInputStream(
					"C:/Users/Jyothsna/Documents/test.txt");
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));//gets the buffered reader
			String strLine;
			//This will Take the File in and Reads it Line By Line
			int i = 0;
			while ((strLine = br.readLine()) != null) {
				i++;
				StringTokenizer st = new StringTokenizer(strLine, " ");
				String matchStr = null;
				String token = null;
				while (st.hasMoreElements()) {
					token = st.nextToken();
					if (token.equalsIgnoreCase(matchStr)//Made it case-insensitive
							&& !(matchStr.equals(null))) {
						System.out.println("Repeated word on line " + i + ":"
								+ token + " " + matchStr);
					}
					matchStr = token;

				}

			}
			// Close the input stream
			in.close();
		} 
		/**
		 * All the exceptions are caught here 
		 */
		catch (Exception e) {
			System.err.println("Error: " + e.getMessage());// Print the error message if any..
		}

	}

}
